﻿#include "epi_bizadd.h"
#include "ui_epi_bizadd.h"

BizAdd::BizAdd(QWidget *parent) :
    ItemAdd(parent),
    ui(new Ui::BizAdd)
{
    ui->setupUi(this);

    table=TD_EPIBOLY_INDEX;
    stuffTable=TD_EPIBOLY_STUFF;
    info = LST_CONTRACTOR;
    plan_id=-1; // 未经过计划的订单
    saveOk=false;

    _uid=QUuid::createUuid().toString();
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
    loadOptions();
}

BizAdd::~BizAdd()
{
    delete ui;
}

void BizAdd::closeEvent(QCloseEvent *)
{
    if(saveOk)
        accept();
}

void BizAdd::loadOptions()
{
    QStringList optList=getOptions(info);
    if(!optList.isEmpty())
    {
        ui->contractor->addItems(optList);
        ui->contractor->addItem(OPT_EDIT);
        ui->contractor->setCurrentIndex(-1);
    }
    else
        ui->contractor->addItem(QStringLiteral("连接错误！"));
}

void BizAdd::updateView(const QString &uid)
{
    model->setTable(stuffTable);
    model->setFilter(QString("uid = '%1'").arg(uid));
    model->select();

    ui->tableView->hideColumn(0);   //id
    ui->tableView->hideColumn(1);   //uid
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("名称"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("数量"));
    ui->tableView->hideColumn(4);   //status
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("备注"));
}

bool BizAdd::saveIndex(const QString &contractor, const QString &name,
                       const QString &number, const QString &request,
                       const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (uid,plan_id,date,contractor,name,number,request,status,remark) values (?,?,?,?,?,?,?,?,?)").arg(table));
    query.addBindValue(_uid);
    query.addBindValue(plan_id);
    query.addBindValue(Env::today);
    query.addBindValue(contractor);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(request);
    query.addBindValue(status);
    query.addBindValue(remark);

    return query.exec();
}

bool BizAdd::saveStuff(const QString &name, const QString &number, const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (uid,name,number,status,remark) values (?,?,?,?,?)").arg(stuffTable));
    query.addBindValue(_uid);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(status);
    query.addBindValue(remark);

    return query.exec();
}

void BizAdd::on_btn_submit_clicked()
{
    if(ui->basePan->isEnabled())
    {
        QString contractor=ui->contractor->currentText();
        if(contractor.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("承包人不能为空！"));
            return;
        }

        QString name=ui->name->text().trimmed();
        QString number=ui->number->text().trimmed();
        if(name.isEmpty() || number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"));
            return;
        }

        QString request=ui->request->text().trimmed();
        QString remark=ui->remark->text().trimmed();
        if(!saveIndex(contractor,name,number,request,remark))
        {
            QMessageBox::warning(this,QStringLiteral("数据库异常"),QStringLiteral("无法保存基本信息！"));
            return;
        }
        pair.first=PAIR_INDEX;

        QString stuff_name=ui->stuff_name->text().trimmed();
        QString stuff_number=ui->stuff_number->text().trimmed();
        if(stuff_name.isEmpty() || stuff_number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("材料名称、数量不能为空！"));
            return;
        }
        QString stuff_remark=ui->stuff_remark->text().trimmed();
        if(saveStuff(stuff_name,stuff_number,stuff_remark))
        {
            updateView(_uid);
            ui->stuff_name->clear();
            ui->stuff_number->clear();
            ui->stuff_remark->clear();

            pair.second=PAIR_DETAIL;

            if(pair.first == PAIR_INDEX && pair.second== PAIR_DETAIL)
            {
                saveOk=true;
                ui->basePan->setEnabled(false);
            }
        }
        else
            QMessageBox::critical(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，提交失败！"));
    }
    else
    {
        QString stuff_name=ui->stuff_name->text().trimmed();
        QString stuff_number=ui->stuff_number->text().trimmed();
        if(stuff_name.isEmpty() || stuff_number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("材料名称、数量不能为空！"));
            return;
        }
        QString stuff_remark=ui->stuff_remark->text().trimmed();
        if(saveStuff(stuff_name,stuff_number,stuff_remark))
        {
            updateView(_uid);
            ui->stuff_name->clear();
            ui->stuff_number->clear();
            ui->stuff_remark->clear();
        }
        else
            QMessageBox::critical(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，提交失败！"));
    }
}

void BizAdd::on_btn_save_clicked()
{
    model->submitAll();
}

void BizAdd::reload()
{
    ui->contractor->clear();
    loadOptions();
}

void BizAdd::on_contractor_currentTextChanged(const QString &text)
{
    if(text == OPT_EDIT)
    {
        OptionEditor editor(this,info);
        editor.exec();
        reload();
    }
}

void BizAdd::genRecordFromStuff(int id, const QString &info)
{
    plan_id=id;

    QSqlQuery _query;
    _query.exec(QString("select name,number,request from %1 where id='%2'")
                .arg(PLAN_EPIBOLY_DETAIL)
                .arg(plan_id));
    _query.next();

    QString name=_query.value(0).toString();
    QString number=_query.value(1).toString();
    QString request=_query.value(2).toString();

    ui->name->setText(name);
    ui->number->setText(number);
    ui->request->setText(request);
    ui->remark->setText(info);
}
